<style scoped lang="scss">
.area-card {
  display: none;
  width: 300px;
}

.area-show-box {
  position: relative;

  &:hover .area-card {
    z-index: 99;
    margin-top: -3px;
    display: block;
    position: absolute;
    right: 0%;
  }
}
</style>
<template>
  <div class="app-container" style="margin-top: -15px;">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="区域名称" prop="names" size="small">
        <el-input size="small" v-model="queryParams.names" placeholder="请输入区域名称" clearable />
      </el-form-item>
      <el-form-item size="small" style="margin-left: -10px;">
        <el-button size="small" type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <div class="area-show-box" style="margin-left: 20px;">
          <div>选中{{ selectAreasLength }}个围栏</div>
          <el-card class="area-card">
            <template #header>
              <div class="card-header">
                <el-button  class="button" type="danger" @click="delAll" text>全部删除</el-button>
              </div>
            </template>
            <el-row v-for="(item,index) in selectAreas" :key="item.id">
              <el-col :span="16">
                {{ item.names }}
              </el-col>
              <el-col :span="8">
                <el-button type="primary" text @click="cancelRow(index)">取消选中</el-button>
              </el-col>
            </el-row>
            
          </el-card>
        </div>
      </el-form-item>
    </el-form>
    <el-table border stripe v-loading="loading" :data="areaList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="电子围栏名称" align="center" prop="names" />
    </el-table>

    <pagination layout="prev, pager, next" v-show="total > 0" :total="total" v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize" @pagination="getList" />
  </div>
</template>

<script setup name="Area">
import { listArea, getArea, delArea, addArea, updateArea } from "@/api/safety/area";
import useSatetyMapStore from '@/store/modules/safetyMap'
const { proxy } = getCurrentInstance();

const areaList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 4,
    names: undefined,
  },
  rules: {
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询电子围栏列表 */
function getList() {
  loading.value = true;
  listArea(queryParams.value).then(response => {
    areaList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

function delAll(){
  safeMapStore.delAllAreasRow()
}

function cancelRow(index) {
  console.log('index',index)
  safeMapStore.delSelectAreasRow(index)
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    names: null,
  };
  proxy.resetForm("areaRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  console.log('selected', selection)
  safeMapStore.setSelectAreas(selection)
}


/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value;
  proxy.$modal.confirm('是否确认删除电子围栏编号为"' + _ids + '"的数据项？').then(function () {
    loading.value = true;
    return delArea(_ids);
  }).then(() => {
    loading.value = true;
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {
  }).finally(() => {
    loading.value = false;
  });
}
getList();
const safeMapStore = useSatetyMapStore();
const selectAreasLength = computed(() => {
  return safeMapStore.selectAreas.length
})
const selectAreas = computed(() => {
  return safeMapStore.selectAreas
})
</script>
